/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.security.exception;

import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.AccessDeniedException;
import org.springframework.security.SpringSecurityMessageSource;

/**
 * Manager to get acei exceptions with common messages.
 */
public class SpringSecurityExceptionsManager
    implements MessageSourceAware
{

  /**
   * the spring security messages.
   */
  private MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();



  /**
   * returns the exception to throws if the access is not granted.
   * 
   * @return the exception to throws if the access is not granted
   */
  public AccessDeniedException getAccessDeniedException()
  {
    return new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied", "Access is denied"));

  }



  /**
   * Set the MessageSource that this object runs in.
   * <p>
   * Invoked after population of normal bean properties but before an init
   * callback like InitializingBean's afterPropertiesSet or a custom
   * init-method. Invoked before ApplicationContextAware's
   * setApplicationContext.
   * 
   * @param messageSource
   *          message sourceto be used by this object
   */
  public void setMessageSource(MessageSource messageSource)
  {
    this.messages = new MessageSourceAccessor(messageSource);
  }

}
